<main class="main list" id="main">
    <div class="main-inner">
        <div class="content categories">
            {{ if .Site.Params.displayListTitle }}
                <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
            {{ end }}
            <div class="tree">
                {{ .Scratch.Delete "categories" }}
                {{ range $.Site.RegularPages }}
                    {{ with .Param "categories" }}
                        {{ $.Scratch.Delete "category" }}
                        {{ range $index, $category := . }}
                            {{ $category := (printf `/%s` ($category | urlize)) }}
                            {{ $.Scratch.Add "category" $category }}
                            {{ $.Scratch.Add "categories" (slice ($.Scratch.Get "category")) }}
                        {{ end }}
                    {{ end }}
                {{ end }}
                {{ $categories := uniq (.Scratch.Get "categories") | sort }}
                {{ range $categories }}
                    {{ $categoryTerms := (split (strings.TrimPrefix "/" .) "/") }}
                    {{ $depth := (len $categoryTerms) }}
                    {{ $padding := (mul (mul $depth 2) 1.2) }}
                    {{ $lastTerm := (delimit (last 1 $categoryTerms) " ") }}
                    {{ $url := urls.Parse $lastTerm }}
                    {{ $path := $url.Path }}
                    {{ with $.Site.GetPage (printf `/categories/%s` $path) }}
                        <p style="padding-left: {{ $padding }}em">
                            <a href="{{ .RelPermalink }}" class="category-item">{{ .LinkTitle | default .Data.Term | default $path }}</a>
                            {{ if $.Site.Params.displayPostsCount }}
                                <span class="category-count">{{ printf "(%d)" (len .Data.category) }}</span>
                            {{ end }}
                        </p>
                    {{ end }}
                    {{ if $.Site.Params.displayPosts }}
                        {{ $context := . }}
                        {{ range $.Site.RegularPages }}
                            {{ $page := . }}
                            {{ with .Param "categories" }}
                                {{ $.Scratch.Delete "category" }}
                                {{ range . }}
                                    {{ $category := (printf `/%s` (. | urlize)) }}
                                    {{ $.Scratch.Add "category" $category }}
                                {{ end }}
                                {{ $category := $.Scratch.Get "category" }}
                                {{ if eq $context $category }}
                                    <p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
                                        <a href="{{ $page.RelPermalink }}" class="category-post">{{ $page.LinkTitle }}</a>
                                    </p>
                                {{ end }}
                            {{ end }}
                        {{ end }}
                    {{ end }}
                {{ end }}
            </div>
        </div>
    </div>
</main>